package com.example.ymm.modules1.service.impl.hl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.dkn.DknForecastProduct;
import com.example.ymm.modules1.entity.ms.VckStock;
import com.example.ymm.modules1.mapper.hl.DknForecastProductMapper;
import com.example.ymm.modules1.query.hl.DknForecastProductQueryCriteria;
import com.example.ymm.modules1.service.dto.dkn.DknForecastStockDto;
import com.example.ymm.modules1.service.dto.dkn.DknNotInOrderDto;
import com.example.ymm.modules1.service.hl.DknForecastProductService;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@DS("slave2")
public class DknForecastProductServiceImpl extends BaseServiceImpl<DknForecastProductMapper, DknForecastProduct> implements DknForecastProductService {

    @Override
    public TableList<DknForecastProduct> list(DknForecastProductQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<DknForecastProduct> arrs = queryAll(criteria);
        PageInfo<DknForecastProduct> pageInfo = new PageInfo<>(arrs);
        TableList<DknForecastProduct> tableList = new TableList(pageInfo);
        return tableList;
    }

    public List<DknForecastProduct> queryAll(DknForecastProductQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DknForecastProduct.class, criteria);
        predicate.orderByDesc("create_time");
        List<DknForecastProduct> result = baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public List<String> importForecastProduct(List<DknForecastProduct> dtos) {
        List<String> erros = new ArrayList<>();
        if (UtilValidate.isEmpty(dtos)){
            throw new YmException("请选择数据!");
        }
        for (int i = 0; i < dtos.size(); i++) {
            DknForecastProduct dknForecastProduct = dtos.get(i);
            String itemName = dknForecastProduct.getItemName();
            String itemCode=itemName.split(" ")[0];
            dknForecastProduct.setItemCode(itemCode);
            String dataSchema = dknForecastProduct.getDataSchema();
            String schemaType = null;
            if(dataSchema.contains("MTO")||dataSchema.contains("MTS Hengli")){
                schemaType = "MTO";
            }else {
                schemaType = "MTS D";
            }
            if(UtilValidate.isNotEmpty(dknForecastProduct.getMtid())){
                dknForecastProduct.setMtid(dknForecastProduct.getMtid().trim());
            }
            dknForecastProduct.setSchemaType(schemaType);
            saveOrUpdate(dknForecastProduct);
        }
//        saveOrUpdateBatch(dtos);
        return erros;
    }


    @Override
    public List<DknNotInOrderDto> getNotInOrders(DknForecastProductQueryCriteria criteria) {

        if(UtilValidate.isEmpty(criteria.getStartDate())){
            Date date = DateKit.dateAdd(3, new Date(), -3);
            criteria.setStartDate(DateKit.dateFormat(date, "yyyy-MM-01"));
        }
        return baseMapper.getNotInOrders(criteria);
    }


    @Override
    public TableList<DknNotInOrderDto> notInOrderTable(DknForecastProductQueryCriteria criteria, YmPageDto ymPage) {
        getPage(ymPage);
        List<DknNotInOrderDto> arrs = getNotInOrders(criteria);
        PageInfo<DknNotInOrderDto> pageInfo = new PageInfo<>(arrs);
        TableList<DknNotInOrderDto> tableList = new TableList(pageInfo);
        return tableList;
    }

    @Override
    public List<DknForecastStockDto> getForecastStock(DknForecastProductQueryCriteria criteria) {
        return baseMapper.getForecastStock(criteria);
    }

    @Override
    public List<DknForecastStockDto> outbillList(DknForecastProductQueryCriteria criteria) {
        return baseMapper.outbillList(criteria);
    }
}
